
package pub.wkq.springbatch._02_params;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author 21129
 */
@Component
@Configuration
public class ParamsJob {
    // 作业启动器
    @Resource
    private JobLauncher jobLauncher;
    // Job 构建工厂  用于构建 Job 对象
    @Resource
    private JobBuilderFactory jobBuilderFactory;

    // Step 构建工厂 用于构建 Step 对象
    @Resource
    private StepBuilderFactory stepBuilderFactory;

    // 构建一个 Step 对象执行的任务（逻辑对象）
//    @Bean
//    public Tasklet tasklet2() {
//        return new Tasklet() {
//            @Override
//            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
//                System.out.println(" params Job ");
//                Map<String, Object> jobParameters =
//                        chunkContext.getStepContext().getJobParameters();
//                System.out.println("jobParameters.get(\"name\") = " + jobParameters.get("name"));
//                // 状态返回
//                return RepeatStatus.FINISHED; // 执行完了
//            }
//        };
//    }
    @Bean
    @StepScope
    public Tasklet tasklet2(@Value("#{jobParameters['name']}") String name) {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println(" params Job ");
                Map<String, Object> jobParameters =
                        chunkContext.getStepContext().getJobParameters();
                System.out.println("jobParameters.get(\"name\") = " + jobParameters.get("name"));
                System.out.println("name = " + name);
                // 状态返回
                return RepeatStatus.FINISHED; // 执行完了
            }
        };
    }
    /**
     * 构建一个 Step 对象
     */
    @Bean
    public Step step2() {
        return stepBuilderFactory
                .get("step1") // 构建一个步骤，传入名称
                .tasklet(tasklet2(null))// tasklet 执行 step 逻辑 类似 Thread()  可以执行 runnable 接口
                .build();
    }
    /**
     *  构造出一个 Job 对象
     */
    @Bean
    public Job job2() {
        return jobBuilderFactory
                .get("params-job")
                .start(step2())
                .build();
    }
}
